/**
* 作者: lichuan
* 日期: 2025-07-28 12:02:29
* 备注: 联盟总动员玩家专属任务
*/
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserMobileTaskInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserMobileTaskInfoModel extends BaseModel implements Model {
	public static final String LISTKEY = "user_id.task_id";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

	protected long id; // id
	protected long userId; // 用户Id
	protected int taskId; // 专属任务Id
	protected long refreshTime; // 下次任务刷新时间
	protected int taskType; // 专属任务类型
	protected long progress; // 进度
	protected int isComplete; // 是否完成

	public void reset() {
	}

	protected String getKey() {
		return "UserMobileTaskInfo." + userId + "." + taskId;
	}

	protected String getListKey() {
		return "UserMobileTaskInfo." + userId;
	}

	protected String getListItemKey() {
		return taskId + "";
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("user_mobile_task_info"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	* 根据入库类型更新
	*/
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	* 实时入库
	*/
	public JsonObject updateNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE_NOW, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	* 实时入队列
	*/
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // id
		obj.put("userId", userId); // 用户Id
		obj.put("taskId", taskId); // 专属任务Id
		obj.put("refreshTime", refreshTime); // 下次任务刷新时间
		obj.put("taskType", taskType); // 专属任务类型
		obj.put("progress", progress); // 进度
		obj.put("isComplete", isComplete); // 是否完成
		return obj;
	}

	public PBUserMobileTaskInfo.Builder takeInitBuilder() {
		PBUserMobileTaskInfo.Builder sb = PBUserMobileTaskInfo.newBuilder();
		if (taskId != 0) {
			sb.setTaskId(taskId);
		}
		if (refreshTime != 0) {
			sb.setRefreshTime(refreshTime);
		}
		if (taskType != 0) {
			sb.setTaskType(taskType);
		}
		if (progress != 0) {
			sb.setProgress(progress);
		}
		if (isComplete != 0) {
			sb.setIsComplete(isComplete);
		}
		return sb;
	}

	public PBUserMobileTaskInfo.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUserMobileTaskInfo.Builder sb = PBUserMobileTaskInfo.newBuilder();
		if (isFieldUpdate("taskId")) {
			sb.setTaskId(taskId);
		}
		if (isFieldUpdate("refreshTime")) {
			sb.setRefreshTime(refreshTime);
		}
		if (isFieldUpdate("taskType")) {
			sb.setTaskType(taskType);
		}
		if (isFieldUpdate("progress")) {
			sb.setProgress(progress);
		}
		if (isFieldUpdate("isComplete")) {
			sb.setIsComplete(isComplete);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public long getUserId() {
		return userId;
	}

	public void putUserId(long userId) {
		if (this.userId != userId) {
			this.userId = userId;
			updateRedisHashField(getKey(), "userId", this.userId);
		}
	}

	public void addUserId(long value) {
		this.userId += value;
		updateRedisHashField(getKey(), "userId", this.userId);
	}

	public int getTaskId() {
		return taskId;
	}

	public void putTaskId(int taskId) {
		if (this.taskId != taskId) {
			this.taskId = taskId;
			updateRedisHashField(getKey(), "taskId", this.taskId);
		}
	}

	public void addTaskId(int value) {
		this.taskId += value;
		updateRedisHashField(getKey(), "taskId", this.taskId);
	}

	public long getRefreshTime() {
		return refreshTime;
	}

	public void putRefreshTime(long refreshTime) {
		if (this.refreshTime != refreshTime) {
			this.refreshTime = refreshTime;
			updateRedisHashField(getKey(), "refreshTime", this.refreshTime);
		}
	}

	public void addRefreshTime(long value) {
		this.refreshTime += value;
		updateRedisHashField(getKey(), "refreshTime", this.refreshTime);
	}

	public int getTaskType() {
		return taskType;
	}

	public void putTaskType(int taskType) {
		if (this.taskType != taskType) {
			this.taskType = taskType;
			updateRedisHashField(getKey(), "taskType", this.taskType);
		}
	}

	public void addTaskType(int value) {
		this.taskType += value;
		updateRedisHashField(getKey(), "taskType", this.taskType);
	}

	public long getProgress() {
		return progress;
	}

	public void putProgress(long progress) {
		if (this.progress != progress) {
			this.progress = progress;
			updateRedisHashField(getKey(), "progress", this.progress);
		}
	}

	public void addProgress(long value) {
		this.progress += value;
		updateRedisHashField(getKey(), "progress", this.progress);
	}

	public int getIsComplete() {
		return isComplete;
	}

	public void putIsComplete(int isComplete) {
		if (this.isComplete != isComplete) {
			this.isComplete = isComplete;
			updateRedisHashField(getKey(), "isComplete", this.isComplete);
		}
	}

	public void addIsComplete(int value) {
		this.isComplete += value;
		updateRedisHashField(getKey(), "isComplete", this.isComplete);
	}

}